// pages/register/register.js
import authService from "../../services/auth.service";
import {
  REGEX_EMAIL,
  REGEX_PASSWORD,
  REGEX_PHONE
} from "../../utils/regexes";

Page({

  /**
   * 页面的初始数据
   */
  data: {
    username: '',
    phone: '',
    email: '',
    password: '',
    confirmPassword: '',
    verifyCode: "",
    verifyCodeKey: "",
    verifyCodeUrl: "",
  },

  updateVerifyCode: async function () {
    try {
      const res = await authService.getVerifyCode();
      this.setData({
        verifyCodeUrl: `${res.data.data?.image}`,
        verifyCodeKey: res.data.data?.key,
      });
    } catch (err) {
      wx.showToast({
        title: err.data && err.data.message || err.errMsg,
        icon: 'error'
      });
      throw err;
    }
  },

  async register() {
    try {
      if (this.data.username == null || this.data.username == "") {
        wx.showToast({
          title: '请输入用户名',
          icon: "error"
        });
        return;
      }
      let res = await authService.checkUsername(this.data.username);
      if (!res.data.data) {
        wx.showToast({
          title: '用户名已存在',
          icon: "error"
        });
        return;
      }
      if (this.data.password == null || this.data.password == "") {
        wx.showToast({
          title: '请输入密码',
          icon: "error"
        });
        return;
      }
      if (!REGEX_PASSWORD.test(this.data.password)) {
        wx.showToast({
          title: '密码格式应为8-18位数字、字母、符号的任意两种组合',
          icon: "error"
        });
        return;
      }
      if (this.data.confirmPassword == null || this.data.confirmPassword == "") {
        wx.showToast({
          title: '请输入确认密码',
          icon: "error"
        });
        return;
      }
      if (this.data.password != this.data.confirmPassword) {
        wx.showToast({
          title: '两次密码输入不一致',
          icon: "error"
        });
        return;
      }
      if (this.data.phone == null || this.data.phone == "") {
        wx.showToast({
          title: '请输入手机号',
          icon: "error"
        });
        return;
      }
      if (!REGEX_PHONE.test(this.data.phone)) {
        wx.showToast({
          title: '手机号格式不正确',
          icon: "error"
        });
        return;
      }
      res = await authService.checkPhone(this.data.phone);
      if (!res.data.data) {
        wx.showToast({
          title: '手机号已存在',
          icon: "error"
        });
        return;
      }
      if (this.data.email == null || this.data.email == "") {
        wx.showToast({
          title: '请输入邮箱',
          icon: "error"
        });
        return;
      }
      if (!REGEX_EMAIL.test(this.data.email)) {
        wx.showToast({
          title: '邮箱格式不正确',
          icon: "error"
        });
        return;
      }
      res = await authService.checkEmail(this.data.email);
      if (!res.data.data) {
        wx.showToast({
          title: '邮箱已存在',
          icon: "error"
        });
        return;
      }
      if (this.data.verifyCode == null || this.data.verifyCode == "") {
        wx.showToast({
          title: '请输入验证码',
          icon: "error"
        });
        return;
      }
      const body = {
        username: this.data.username,
        phone: this.data.phone,
        email: this.data.email,
        password: this.data.password,
        verifyCodeKey: this.data.verifyCodeKey,
        verifyCode: this.data.verifyCode,
      }
      await authService.register(body);

      wx.showToast({
        title: '注册成功',
        icon: "success"
      });
      wx.navigateTo({
        url: '/pages/login/login',
      });
    } catch (err) {
      wx.showToast({
        title: err.data && err.data.message || err.errMsg,
        icon: 'error'
      });
      throw err;
    } finally {}
  },

  login() {
    wx.navigateTo({
      url: '/pages/login/login',
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.updateVerifyCode();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})